package com.example.whereismystuff.views;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.example.whereismystuff.R;
import com.example.whereismystuff.classes.Item;
import com.example.whereismystuff.classes.ItemDataBaseAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This activity allows the user to view the information of the item without editing the
 * information
 * @author NullPointers
 *
 */
public class ViewAnItem extends Activity {
	private TextView user, name, i_zipCode, description, i_tags;
	private RadioGroup rdbGp1;
	private String itemName, itemDescription, foundString, username, zipCode, tempItemName, id;
	private Button seeMap;
	
	private ArrayList<String> tags;
	private Item item;
	private Intent i;
	private Uri outputFileUri;


	ItemDataBaseAdapter itemDataBaseAdapter;

	private final String DIALOG_MSG = "Please Input a Name";
	private ImageView imageView;


	/**
	 * Method used to Instantiate Object and set the Layout for the Activity
	 * @param Bundle
	 */

	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_an_item);

		itemDataBaseAdapter=new ItemDataBaseAdapter(this);
		itemDataBaseAdapter=itemDataBaseAdapter.open();
		
		imageView = (ImageView) findViewById(R.id.v_imageView);
		
		name = (TextView) findViewById(R.id.v_itemName);
		i_zipCode = (TextView) findViewById(R.id.v_zipCode);
		description = (TextView) findViewById(R.id.v_Description);
		i_tags = (TextView) findViewById(R.id.v_tags);
		user = (TextView) findViewById(R.id.v_user);
		rdbGp1 = (RadioGroup) findViewById(R.id.rdbGp1);
		Bundle extras = getIntent().getExtras();
		if(extras != null){
			if(extras.getString("ItemName") != null){
				itemName = extras.getString("ItemName");
				name.setText(itemName);
			}
			if(extras.getString("Description") != null){
				itemDescription = extras.getString("Description");
				description.setText(itemDescription);
			}
			
			if(extras.getString("Found") != null){
				foundString = extras.getString("Found");
			}
			if(extras.getString("Image") != null){
				String outputBitmap = extras.getString("Image");
				imageView.setImageBitmap(decodeBitmap(outputBitmap, 200, 200));
			}
			if(extras.getString("ZipCode") != null){
				zipCode = extras.getString("ZipCode");
				i_zipCode.setText(zipCode);
			}
			if(extras.getString("Username") != null){
				username = extras.getString("Username");
				Toast.makeText(getApplicationContext(), username, Toast.LENGTH_LONG).show();
			}
			id = extras.getString("id");
		}
		String id = extras.getString("id");
		if(foundString.equals("true")) {
			((CompoundButton) rdbGp1.getChildAt(0)).setChecked(true);
		}
		else
			((CompoundButton) rdbGp1.getChildAt(1)).setChecked(true);
		
		user.setText(user.getText() + " " + username);
		
		tags = itemDataBaseAdapter.getSingleEntryTags(itemName);
		if(tags.get(0).equals("true"))
			i_tags.setText(i_tags.getText() + "\n - Book");
		if(tags.get(1).equals("true"))
			i_tags.setText(i_tags.getText() + "\n - Electronic");
		if(tags.get(2).equals("true"))
			i_tags.setText(i_tags.getText() + "\n - Personal");
		
		seeMap = (Button)findViewById(R.id.seeMap);
	}

	public void onBackPressed() {
		try{
			Class c = Class.forName("com.example.whereismystuff.views.ItemViewer");
			i = getIntent();
			Bundle extras = i.getExtras();
			i = new Intent(getApplicationContext(), c);
			i.putExtras(extras);
			startActivity(i);

			finish();
		} catch (ClassNotFoundException e){
			e.printStackTrace();
		}
	}

	
	public static Bitmap decodeBitmap(String id,
			int width, int height) {

		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(id, options);

		// Calculate inSampleSize
		options.inSampleSize = calcSize(options, width, height);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(id, options);
	}

	public static int calcSize(BitmapFactory.Options options, int width, int height) {
		final int tempHeight = options.outHeight;
		final int tempWidth = options.outWidth;
		int size = 2;

		if (tempHeight > height || tempWidth > width) {
			if (tempWidth > tempHeight) {
				size = Math.round((float)tempHeight / (float)height);
			} else {
				size = Math.round((float)tempWidth / (float)width);
			}
		}
		return size;
	}
	
	public void seeMap(View v){
		try{
			Class c = Class.forName("com.example.whereismystuff.views.GoogleMapsView");
			i = new Intent(getApplicationContext(), c);
			Bundle extras = new Bundle();
			extras.putString("ItemName", itemName);
			i.putExtras(extras);
			startActivity(i);
		} catch (ClassNotFoundException e){
			e.printStackTrace();
		}
	}

}
